package com.xiangban.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {




    /**
     * 普通的topic交换机(超时订单)
     */
    public static final String ORDER_EXCHANGE ="order_exchange";

    /**
     * 路由key(超时订单)
     */
    public static final String  ORDER_ROUTING_KEY ="order_routing_key";

    /**
     * 普通队列，绑定死信交换机(超时订单)
     */
    public static final String ORDER_QUEUE ="order_queue";


    /**
     * 死信交换机(超时订单)
     */
    public static final String DOCTOR_ORDER_DEAD_EXCHANGE = "doctor_order_dead_exchange";
    /**
     * 死信队列(超时订单)
     */
    public static final String DOCTOR_ORDER_DEAD_QUEUE = "doctor_order_dead_queue";
    /**
     *进入死信队列的路由key(超时订单)
     */
    public static final String DOCTOR_ORDER_ROUTING_KEY = "doctor_order_routing_key";


    /**
     * 创建普通交换机(超时订单)
     * @return
     */
    @Bean
    public Exchange newMerchantExchange(){

        return new TopicExchange(ORDER_EXCHANGE,true,false);
    }

    /**
     * 创建普通队列(超时订单)
     * @return
     */
    @Bean
    public Queue newMerchantQueue(){
//        HashMap<String, Object> map = new HashMap<>();
//        //消息过期后，进入到死信交换机
//        map.put("x-dead-letter-exchange",LOCK_ORDER_DEAD_EXCHANGE);
//
//        //消息过期后，进入到死信交换机的路由key
//        map.put("x-dead-letter-routing-key",LOCK_ORDER_ROUTING_KEY);
//
//        //过期时间，单位毫秒


        return  QueueBuilder.durable(ORDER_QUEUE).withArgument("x-dead-letter-exchange",DOCTOR_ORDER_DEAD_EXCHANGE).
                withArgument("x-dead-letter-routing-key",DOCTOR_ORDER_ROUTING_KEY).build();
    }

    /**
     * 普通交换器绑定普通队列(超时订单)
     * @return
     */
    @Bean
    public Binding newMerchantBinding(){
        return new  Binding(ORDER_QUEUE,Binding.DestinationType.QUEUE,
                ORDER_EXCHANGE,ORDER_ROUTING_KEY,null);
    }

    /**
     * 创建死信交换机(超时订单)
     * @return
     */
    @Bean
    public Exchange lockMerchantDeadExchange(){
        return new TopicExchange(DOCTOR_ORDER_DEAD_EXCHANGE,true,false);
    }

    /**
     * 创建死信队列(超时订单)
     * @return
     */
    @Bean
    public Queue lockMerchantDeadQueue(){
        return QueueBuilder.durable(DOCTOR_ORDER_DEAD_QUEUE).build();
    }

    /**
     * 死信交换机绑定死信队列(超时订单)
     * @return
     */
    @Bean
    public Binding lockMerchantBinding(){
        return new Binding(DOCTOR_ORDER_DEAD_QUEUE,Binding.DestinationType.QUEUE,
                DOCTOR_ORDER_DEAD_EXCHANGE,DOCTOR_ORDER_ROUTING_KEY,null);
    }
















    @Bean
    DirectExchange orderDirect() {//(死信交换机)
        return (DirectExchange) ExchangeBuilder
                .directExchange("order")
                .durable(true)
                .build();
    }

    @Bean
    DirectExchange orderTtlDirect() {//(普通交换机)
        return (DirectExchange) ExchangeBuilder
                .directExchange("ttl.order")
                .durable(true)
                .build();
    }

    @Bean
    public Queue orderQueue() {//(死信队列)
        return new Queue("order.cancel");
    }

    @Bean
    public Queue orderTtlQueue() {//(普通队列)
        return QueueBuilder
                .durable("delay.order.cancel")
                .withArgument("x-dead-letter-exchange", "order")
                .withArgument("x-dead-letter-routing-key", "order.routingKey")//到期后转发的路由键
                .build();
    }

    @Bean
    Binding orderBinding(DirectExchange orderDirect, Queue orderQueue){
        return BindingBuilder
                .bind(orderQueue)
                .to(orderDirect)
                .with("order.routingKey");
    }

    @Bean
    Binding orderTtlBinding(DirectExchange orderTtlDirect, Queue orderTtlQueue){
        return BindingBuilder
                .bind(orderTtlQueue)
                .to(orderTtlDirect)
                .with("ttl.order.routingKey");//交换机绑定队列的路由key
    }
}
